
#include "enemy.h"
#include<QUrl>

//Enemy构造函数(初始图片地址，图片缩放比例，受击图片地址，移动图片地址一，移动图片地址二，攻击图片地址，移动加成，攻击加成，受击音效地址，死亡音效地址，敌人类型 ，攻击音效地址一，攻击音效地址二，攻击音效地址三
Enemy::Enemy(QString pixPath,qreal scale,QString apixPath,QString m1Path,QString m2Path,QString aPath,int madd,int aadd,QString beUrl,QString fUrl,int eflag,QString a1Url,QString a2Url,QString a3Url)
{
    this->health=100;//设置初始属性
    this->damage=40;
    this->moveAddition=madd;
    this->attackAddition=aadd;
    this->oPixPath=pixPath;
    this->aPixPath=apixPath;
    this->move1Path=m1Path;
    this->move2Path=m2Path;
    this->attackPath=aPath;
    QPixmap pix(oPixPath);
    this->enemy=new QGraphicsPixmapItem;
    this->enemy->setPixmap(pix);
    this->enemy->setScale(scale);
    this->enemy->setPos(0,460);
    this->width=pix.width()*scale;
    this->health=pix.height()*scale;

    this->attackTimer=new QTimer;
    this->moveTimer=new QTimer;

    this->beattackUrl=beUrl;

    this->enemyflag=eflag;

    this->output=new QAudioOutput(this);//加载音效
    this->beattack=new QMediaPlayer(this);
    this->beattack->setSource(QUrl(this->beattackUrl));
    this->beattack->setAudioOutput(output);
    this->beattack->setLoops(1);

    this->failUrl=fUrl;
    this->output=new QAudioOutput(this);
    this->fail=new QMediaPlayer(this);
    this->fail->setSource(QUrl(this->failUrl));
    this->fail->setAudioOutput(output);
    this->fail->setLoops(1);

    this->output=new QAudioOutput(this);
    this->beattacked=new QMediaPlayer(this);
    this->beattacked->setSource(QUrl("qrc:/resource/beattacked.mp3"));
    this->beattacked->setAudioOutput(output);
    this->beattacked->setLoops(1);

    if(this->enemyflag==1)//enemy类型为1，则加载三个攻击音效
    {
        this->attackUrl1=a1Url;
        this->attackUrl2=a2Url;
        this->attackUrl3=a3Url;

        this->output=new QAudioOutput(this);
        this->attack1=new QMediaPlayer(this);
        this->attack1->setSource(QUrl(this->attackUrl1));
        this->attack1->setAudioOutput(output);
        this->attack1->setLoops(1);

        this->output=new QAudioOutput(this);
        this->attack2=new QMediaPlayer(this);
        this->attack2->setSource(QUrl(this->attackUrl2));
        this->attack2->setAudioOutput(output);
        this->attack2->setLoops(1);

        this->output=new QAudioOutput(this);
        this->attack3=new QMediaPlayer(this);
        this->attack3->setSource(QUrl(this->attackUrl3));
        this->attack3->setAudioOutput(output);
        this->attack3->setLoops(1);
    }

}

QRectF Enemy::boundingRect() const//碰撞体积
{
    return QRectF(this->enemy->x(),this->enemy->y(),this->enemy->x()+this->width,this->enemy->y()+this->height);
}

void Enemy::attack(Character* lumine)//攻击函数
{
    if(!this->attackTimer->isActive()&&!lumine->attackTimer->isActive()&&lumine->health>=0)
    {
        this->attackTimer->start(100);
        if(this->attackPath!=" ")
        {
            QPixmap apix(this->attackPath);
            if(this->dest==0)
                apix=apix.transformed(QTransform().scale(-1, 1));
            this->enemy->setPixmap(apix);
        }

        if(this->enemyflag==1)
        {
            int x=rand()%6+1;
            if(x==1)
            {
                this->attack1->play();
            }
            else if(x==2)
            {
                this->attack2->play();
            }
            else if(x==3)
            {
                this->attack3->play();
            }
        }

    if(this->enemy->collidesWithItem(lumine->pose))
    {
            lumine->health-=this->damage*this->attackAddition;
            qDebug()<<"health:"<<lumine->health;
        if(lumine->health<=0)
            emit this->failed();
        lumine->beAttacked();
    }
    QTimer::singleShot(500,this,[=](){
        this->enemy->setPixmap(QPixmap(this->oPixPath));
        QTimer::singleShot(600,this,[=](){
            this->attackTimer->stop();
        });
    });
    }

}

int Enemy::moveflag=0;

void Enemy::move(Character* lumine)//移动函数
{
    if(this->health<=0)
    return;
    if(!this->enemy->collidesWithItem(lumine->pose))
    {
    if(this->move1Path!=" "&&this->moveflag%2==0)
    {
        QPixmap mpix1(this->move1Path);
        if(this->dest==0)
            mpix1=mpix1.transformed(QTransform().scale(-1, 1));
        this->enemy->setPixmap(mpix1);
        this->moveflag++;
    }
    else if(this->move2Path!=" "&&this->moveflag%2==1)
    {
        QPixmap mpix2(this->move2Path);
        if(this->dest==0)
            mpix2=mpix2.transformed(QTransform().scale(-1, 1));
        this->enemy->setPixmap(mpix2);
        this->moveflag++;
        if(this->moveflag>=2)
            this->moveflag=0;
    }
    this->enemy->setPos(this->enemy->x()+dx*30*this->moveAddition,this->enemy->y());
    }
    else
    {
        this->attack(lumine);
    }
}

